package com.sumover.controller;

import com.sumover.POJO.Game;
import com.sumover.POJO.GameDetail;
import com.sumover.POJO.GameLink;
import com.sumover.POJO.GameType;
import com.sumover.Service.GameService;
import com.sumover.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@RequestMapping("/game")
@RestController
public class GameController {

    @Autowired
    private GameService gameService;

    /**
     * 根据id获取游戏信息
     *
     * @param gid
     * @return
     */
    @RequestMapping("/getGame")
    public ResultVo getGame(@RequestBody Integer gid) {
        Game game = gameService.getGame(gid);
        return ResultVo.success("game returned", game);
    }

    /**
     * 获得所有游戏列表
     *
     * @return
     */
    @RequestMapping("/getAllGames")
    public ResultVo getAllGames() {
        List<Game> allGames = gameService.queryAllGames();
        List<List> results = new LinkedList<>();
        for (Game game : allGames) {
            List turple = new LinkedList();
            turple.add(game);
            turple.add(gameService.getGameDetail(game));
            results.add(turple);
        }
        return ResultVo.success("return all games", results);
    }

    /**
     * 获取所有的类型
     *
     * @return
     */
    @RequestMapping("/getAllGameTypes")
    public ResultVo getAllGameTypes() {
        List<GameType> allGameTypes = gameService.getAllGameTypes();
        List<List> result = new LinkedList<>();
        for (GameType gameType : allGameTypes) {
            List turple = new LinkedList();
            turple.add(gameType);
            turple.add(gameService.getGameByTypes(gameType));
            result.add(turple);
        }
        return ResultVo.success("all types returned", result);
    }

    /**
     * 获取游戏详情
     *
     * @param game
     * @return
     */
    @RequestMapping("/getGameDetail")
    public ResultVo getGameDetail(@RequestBody Game game) {
        GameDetail gameDetail = gameService.getGameDetail(game);
        return ResultVo.success("return game `" + game.getGamename() + "` details", gameDetail);
    }

    /**
     * 一个简单的统计
     * 获取平均分
     *
     * @param game
     * @return
     */
    @RequestMapping("/getGameScore")
    public ResultVo getGameScore(@RequestBody Game game) {
        Double gameScore = gameService.getGameScore(game);
        return ResultVo.success("query game score", gameScore);
    }

    /**
     * 获取游戏评分统计数据
     *
     * @param game
     * @return
     */
    @RequestMapping("/getGameScoreStatistics")
    public ResultVo getGameScoreStatistics(@RequestBody Game game) {
        HashMap<Integer, Integer> gameScoreStatistics = gameService.getGameScoreStatistics(game);
        return ResultVo.success("return game score statistics", gameScoreStatistics);
    }

    /**
     * 获取所有游戏的链接
     *
     * @param game
     * @return
     */
    @RequestMapping("/getGameLinks")
    public ResultVo getGameLinks(@RequestBody Game game) {
        List<GameLink> gameLinks = gameService.getGameLinks(game);
        return ResultVo.success("game link returned", gameLinks);
    }


    /**
     * 根据类型获取游戏
     *
     * @param type
     * @return
     */
    @RequestMapping("/getGamesByType")
    public ResultVo getGamesByTypes(@RequestBody GameType type) {
        List<Game> games = gameService.getGameByTypes(type);
        return ResultVo.success("return games by type", games);
    }

    @RequestMapping("/getGameByName")
    public ResultVo getGameByName(@RequestBody String gamename) {
        Game gameByName = gameService.getGameByName(gamename);
        if (gameByName == null)
            return ResultVo.error("no game");
        else return ResultVo.success("game query success", gameByName);
    }


}
